package com.jxb.netty.network.netty.http.server;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

public class BusiHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String result = "";
        FullHttpRequest httpRequest = (FullHttpRequest) msg;
        System.out.println(httpRequest.headers());
        try {
            //获取路径
            String path = httpRequest.getUri();
            //获取body
            String body = httpRequest.content().toString(CharsetUtil.UTF_8);
            //获取请求方法
            HttpMethod method = httpRequest.method();
            System.out.println("接收到:"+method+" 请求");
            //判断非法请求
            if (!"/test".equals(path)) {
                result = "非法请求!"+path;
                send(ctx,result, HttpResponseStatus.BAD_REQUEST);
                return;
            }
            //GET请求
            if (HttpMethod.GET.equals(method)) {
                System.out.println("body:"+body);
                result="GET请求,应答:"+RespConstant.getNews();
                send(ctx,result, HttpResponseStatus.OK);
                return;
            }
            //如果是其他类型请求，如post
            if(HttpMethod.POST.equals(method)){
                //接受到的消息，做业务逻辑处理...
                //....
                return;
            }
        }catch (Exception e) {
            System.out.println("处理请求失败!");
            e.printStackTrace();
        }finally {
            httpRequest.release();
        }
    }

    private void send(ChannelHandlerContext ctx, String result, HttpResponseStatus badRequest) {
        DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, badRequest, Unpooled.copiedBuffer(result, CharsetUtil.UTF_8));
        response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain;charset=UTF-8");
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }
}
